<?php
# header of each page
function head($title='', $meta_description='', $meta_keywords='', $active='') {
header("Content-type: text/html; charset=UTF-8");
?>
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
<meta name="description" content="<?=$meta_description?>" />

<link href='http://fonts.googleapis.com/css?family=Open+Sans:300|Open+Sans+Condensed:300,700&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" type="text/css" href="<?=HTML_BASE?>css/reset.css">
<link rel="stylesheet" type="text/css" href="<?=HTML_BASE?>css/style.css">
<link href="<?=HTML_BASE?>css/dark.css" rel="stylesheet" type="text/css" />

<script src="<?=HTML_BASE?>js/modernizr.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="<?=HTML_BASE?>js/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="<?=HTML_BASE?>js/jquery.timeline.min.js"></script>
<script src="<?=HTML_BASE?>js/script.js" type="text/javascript"></script>
</head>

<body>
<div id="popup"></div>
<header>
	<div class="line3 yellow sm"></div>
    <div class="line3 orange sm"></div>
    <div class="line3 dark-orange sm"></div>
    
    <div id="menu">
    	
        
        <?php
			   
			 
			 $sql= "SELECT menuitems FROM menus WHERE menu_id = '2'";
			 $result = query($sql);
			 $row_m = mysql_fetch_object($result);
			 
			 $arr =  json_c_decode($row_m->menuitems);
			 function create_ul($ar,$first)
			 {
					$menu_id = 2;
					echo '<ul>';
					
					
					foreach ($ar as $key => $value) {
					 
					
						$item = $value['id'];
						
						$sqlm = "SELECT parent_id,title FROM  menu_description 
							  WHERE menu_id = '".$menu_id."'AND item_id = '".$item."'
							  AND lang_id = '".$_SESSION['lang_id']."'
							 ";
					
						$rowm = mysql_fetch_object(query($sqlm)); 
					
						$sql_sef = "SELECT sef_url FROM  sef_url 
							  WHERE query = '".$rowm->parent_id."' AND tamplate='page' AND lang_id = '".$_SESSION['lang_id']."'
							 ";
						$row_sef = mysql_fetch_object(query($sql_sef));
						
						$active = '';
						if($row_sef->sef_url == $_GET['prm1']) $active = 'class="active"';
						$parent = '';
						if($value['children'])$parent = 'class="isparent"';
						
						$href = 'href="#"';
						if(!$value['children']) $href = 'href="'.HTML_BASE.$_SESSION['lang'].'/'.$row_sef->sef_url.'"';
						
						
						
						echo '<li '.$parent.'><a '.$active.' '.$href.' >'.$rowm->title.'</a>';
						 
						if($value['children']) create_ul($value['children'],false);
						 
						echo '</li>';
					}
						
						
						echo '</ul>';
					
					
			 }
			 
			 create_ul($arr,true);
					
					
					?>
    </div>
    
    <div id="logo">
    	<a href="<?=HTML_BASE.$_SESSION['lang'].'/'?>"></a>
    </div>
    
    <div id="lang-choice">
    		<a class="<?=(($_SESSION['lang']=='bg')?'active':'')?>" href="<?=HTML_BASE?>bg/">BG</a>
            <a class="<?=(($_SESSION['lang']=='en')?'active':'')?>"  href="<?=HTML_BASE?>en/">EN</a>
     </div>
     
</header>
            
			
	
<?php	
}

# footer of each page
function foot($tmp='') {
	
	$sql =	"SELECT
			  d.description	
		  FROM
			  pages AS c,
			  page_description AS d
		  WHERE
			  c.page_id = d.page_id
			  AND
			  d.lang_id = '".$_SESSION['lang_id']."'
			  AND
			  c.page_id = '6' 
			  ";
$result = query($sql);
$rowf = mysql_fetch_object($result);
?>
		
<section id="partners">
	<?=$rowf->description?>
</section>

<footer>
	<div class="line3 yellow sm"></div>
    <div class="line3 orange sm"></div>
    <div class="line3 dark-orange sm"></div>
    
    <div id="fcontacts">
    	<a class="mail-icon" href="mailto:office@fairfracht.com">office@fairfracht.com</a>
        <a class="phone-icon" href="tel:+35929292681">+ 359 2 929 26 81</a>
    </div>
    <div id="copy">&copy; <?=date('Y')?> Fair Fracht</div>
</footer>
</body>
</html>
        
<?php
}

# a redirecting function that adds the session ID if cookies are disabled and stops the execution of the script
function redirect($location) {
	if (!$location) error("Missing arguments.");

	$a = parse_url($location);
	if (!isset($a['query'])) {
		$a['query'] = '';
	}
	
	$session_delimiter = ($a['query'])?'&':'?';
	header("Location: $location".addsession($session_delimiter));
	exit;
}

# adding the session_id when cookies are disabled
function addsession($symbol="?") {
	return (!isset($_COOKIE[session_name()])&&session_id())?$symbol.session_name()."=".session_id():"";
}

function escape($string,$encoding=false) {
  return mysql_real_escape_string( (($encoding)?encoding($string):$string) );
}

function encoding($string) {
  return iconv('ISO-8859-1', 'UTF-8', $string);
}

function sth($string) {
  return htmlspecialchars(stripslashes($string));
}

# checks whether an email is valid or not
function checkEmail($email) {
	return preg_match("/^[A-z0-9\.\-]+@(?:[A-z0-9\-]+\.)+[A-z0-9]{1,6}$/", $email);
}

# Cut a text to specified chracters length
function text_format($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, ' ', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}

function text_format_blog($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, '<hr />', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}


# general error function
function error($s="An error occured.") {
  if (!headers_sent()) {
    head("Error");
    echo "<table cellpadding=4 width=".SITE_WIDTH." align=center border=0><tr><td align=center class=title>$s</td></tr></table>";
    foot();
  }
  else
    echo $s;
  exit;
}

# debug function for dumping a variable value
function pp($var, $stop=false) {
  echo "<pre>";
  print_r($var);
  echo "</pre>";
  if ($stop) exit;
}

# Generate numbers
function generateNumber () {

  # start with a blank number
  $number = "";

  # define possible characters
  $possible = "0123456789"; 

  # pick a random character from the possible ones
  $number = substr($possible, mt_rand(0, strlen($possible)-1), 1);

  # done!
  return $number;

}



function json_c_decode($json, $assoc = true, $depth = 512, $options = 0) {
			
				$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
				
				if(version_compare(phpversion(), '5.4.0', '>=')) {
					$json = json_decode($json, $assoc, $depth, $options);
				}
				elseif(version_compare(phpversion(), '5.3.0', '>=')) {
					$json = json_decode($json, $assoc, $depth);
				}
				else {
					$json = json_decode($json, $assoc);
				}
			
				return $json;
		}



?>